﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameBase;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GalaxyPresident
{
    public abstract class Ammo : VisibleObject
    {
        protected PlanetObjectOwner owner;
        protected Vector2 _targetPostion;
        protected float maxDistance = 1000f;
        protected float speed;
        protected bool isFiring = false;
        protected float _power;
        protected PlanetObjectOwner _targetObj;
        protected bool _isAutoChase = false;
        protected VisibleObject _ammoEffect;
        protected bool isHitOnTarget = false;

        public bool IsAutoChase
        {
            get { return _isAutoChase; }
            set { _isAutoChase = value; }
        }

        public float Power
        {
            get { return _power; }
            set { _power = value; }
        }

        public Vector2 TargetPostion
        {
            get { return _targetPostion; }
            set { _targetPostion = value; }
        }

        public Ammo(PlanetObjectOwner owner)
        {
            this.owner = owner;           
        }

        public virtual Ammo clone()
        {
            return null;
        }

        private void fireTarget(Vector2 pos, Vector2 tarPos)
        {  
            Vector2 temp = (tarPos - pos);
            temp.Normalize();
            _targetPostion = temp * maxDistance + pos;
        }

        private void fireTarget(Vector2 pos, PlanetObjectOwner obj)
        {
            _targetPostion = obj.Position;
        }

        public void fireTarget(Vector2 pos, PlanetObjectOwner obj, bool isAutoChase)
        {
            _targetObj = obj;
            isFiring = true;
            Position = pos;
            _isAutoChase = isAutoChase;

            if (isAutoChase)
                fireTarget(pos, obj);
            else
                fireTarget(pos, obj.Position);

            Rotate = Helper.getAngleBetween(pos, _targetPostion);
            if (owner.Parent != null)
            {
                owner.Parent.AddChild(this);
            }
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (isFiring)
            {
                updateAmmoPosition();
                updateAmmoEffect();

                if (isHitOnTarget)
                {
                    _currentTotalTime += gameTime.ElapsedGameTime.Milliseconds;
                    if (_currentTotalTime >= 1000)
                    {
                        _currentTotalTime = 0;
                        Parent.RemoveChild(_ammoEffect);
                        Parent.RemoveChild(this);
                    }
                }

                if (position == TargetPostion && Parent!= null)
                    Parent.RemoveChild(this);
            }
        }

        private void updateAmmoEffect()
        {
            float d = (Position - _targetObj.Position).Length();
            if (d < 10 && !isHitOnTarget)
            {
                startAmmoEffect();
                isHitOnTarget = true;
                isVisible = false;
            }
        }

        private void updateAmmoPosition()
        {
            if (_isAutoChase)
            {
                _targetPostion = _targetObj.Position;
                Rotate = Helper.getAngleBetween(Position, _targetPostion);
            }
            Position = MotionEffects.Move(position, _targetPostion, speed);
        }

        private void startAmmoEffect()
        {
            _ammoEffect.Position = Position - new Vector2(10, 10) + 20 * (float)Constant.random.NextDouble() * new Vector2(1, 1);
            //_ammoEffect.Position = _targetObj.Position;
            _ammoEffect.Depth = 0.09f;
            _ammoEffect.Sprite.SetOriginPosition(ORIGIN_POSITION.Center);
            Parent.AddChild(_ammoEffect);

            _targetObj.beHurtWithPower(_power);
        }
    }
}
